!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

recursive subroutine block_destroy (current)

   use parameters
   use block_define

   implicit none

   type(block), pointer, intent(inout) :: current

   integer(4) :: ci, i_level
   type(block), pointer :: child

!----------------------------------------------------------------------------------------------------------------------------------------------|
   if (associated(current%child)) then
      do ci = 1, current%nc
         child => current%child(ci)
         call block_destroy (child)
      enddo
   endif

!----------------------------------------------------------------------------------------------------------------------------------------------|
!  when the pointer of a structure is killed, the space of all of its members is also killed.
!----------------------------------------------------------------------------------------------------------------------------------------------|
   if (associated(current%child)) then
      do i_level = 1, n_levels
         if (associated (current%neigh(1, i_level)%pblock)) then
            current%neigh(1, i_level)%pblock%neigh(2, current%lv + 1)%pblock => null()
            current%neigh(1, i_level)%pblock%neigh_lv(2) = current%lv
         endif
         if (associated (current%neigh(2, i_level)%pblock)) then
            current%neigh(2, i_level)%pblock%neigh(1, current%lv + 1)%pblock => null()
            current%neigh(2, i_level)%pblock%neigh_lv(1) = current%lv
         endif
      enddo
      deallocate (current%child)
      n_blocks = n_blocks - current%nc
      current%nc = 0
   endif

!   current%neighbor(1)
!   current%neighbor(1)

!   if (associated(current%pp)) deallocate (current%pp)
!   if (associated(current%pf)) deallocate (current%pf)
!   if (associated(current%pb)) deallocate (current%pb)

!   if (allocated(current%x)) deallocate (current%x)
!   if (allocated(current%ro)) deallocate (current%ro)
!   if (allocated(current%mx)) deallocate (current%mx)
!   if (allocated(current%en)) deallocate (current%en)
!   if (allocated(current%child(ci)%ro_old)) deallocate (current%child(ci)%ro_old)
!   if (allocated(current%child(ci)%mx_old)) deallocate (current%child(ci)%mx_old)
!   if (allocated(current%child(ci)%en_old)) deallocate (current%child(ci)%en_old)
!   if (allocated(current%child(ci)%flux_ro)) deallocate (current%child(ci)%flux_ro)
!   if (allocated(current%child(ci)%flux_mx)) deallocate (current%child(ci)%flux_mx)
!   if (allocated(current%child(ci)%flux_en)) deallocate (current%child(ci)%flux_en)
!   if (allocated(current%child(ci)%flag)) deallocate (current%child(ci)%flag)
!----------------------------------------------------------------------------------------------------------------------------------------------|

   return
end subroutine block_destroy

